package com.vanhon.engine.decrypt;

import android.util.Log;
import java.util.Locale;

/* loaded from: classes.dex */
public class Decrypt {
    private static final long EXTRA_KEY = -1981399254;
    private static final int INVALID_HANDLE = 0;
    private static final String PASSWD = "**3450982!!!UKX!@#$-=-**";
    private static final String TAG = "Decrypt";
    private long mDecHandle;
    private Object mLock = new Object();

    private static native void close(long j);

    private static native long decode(long j, long j2, byte[] bArr, byte[] bArr2);

    private static native long decodeExt(long j, long j2, byte[] bArr, int i, int i2, byte[] bArr2, int i3, int i4);

    private static native long open(byte[] bArr, String str, long j);

    static {
        System.loadLibrary("DecryptNative");
    }

    public static boolean isEncrypted(byte[] bArr, int i) {
        if (i < bArr.length) {
            byte[] bArr2 = new byte[i];
            for (int i2 = 0; i2 < i; i2++) {
                bArr2[i2] = bArr[i2];
            }
            bArr = bArr2;
        }
        long open = open(bArr, PASSWD, EXTRA_KEY);
        close(open);
        return open != 0;
    }

    public Decrypt(byte[] bArr) {
        this.mDecHandle = 0L;
        this.mDecHandle = open(bArr, PASSWD, EXTRA_KEY);
        Log.e(TAG, String.format(Locale.US, "open head len: %d, head0: 0x%X, mDecHandle: 0x%X", Integer.valueOf(bArr.length), Byte.valueOf(bArr[0]), Long.valueOf(this.mDecHandle)));
    }

    public boolean initCheck() {
        boolean z;
        synchronized (this.mLock) {
            z = this.mDecHandle != 0;
        }
        return z;
    }

    public void release() {
        synchronized (this.mLock) {
            if (this.mDecHandle != 0) {
                close(this.mDecHandle);
                this.mDecHandle = 0L;
            }
        }
    }

    public long decrypt(byte[] bArr, long j, byte[] bArr2) {
        synchronized (this.mLock) {
            int i = 0;
            if (this.mDecHandle == 0) {
                while (i < bArr.length && i < bArr2.length) {
                    bArr2[i] = bArr[i];
                    i++;
                }
                return i;
            }
            long decode = decode(this.mDecHandle, j, bArr, bArr2);
            Log.e(TAG, String.format(Locale.US, "decrypt streamPos: %d, srcBuf0:0x%X, return: %d", Long.valueOf(j), Byte.valueOf(bArr[0]), Long.valueOf(decode)));
            return decode;
        }
    }

    public long decrypt(long j, byte[] bArr, int i, int i2, byte[] bArr2, int i3, int i4) {
        synchronized (this.mLock) {
            if (this.mDecHandle != 0) {
                long decodeExt = decodeExt(this.mDecHandle, j, bArr, i, i2, bArr2, i3, i4);
                Log.d(TAG, String.format(Locale.US, "decodeExt streamPos: %d, srcBuf0: 0x%X, srcOff: %d, srcLen: %d, dstOff: %d, dstLen: %d, return: %d", Long.valueOf(j), Byte.valueOf(bArr[0]), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3), Integer.valueOf(i4), Long.valueOf(decodeExt)));
                return decodeExt;
            }
            int i5 = i2;
            if (i5 > i4) {
                i5 = i4;
            }
            for (int i6 = 0; i6 < i5; i6++) {
                bArr2[i3 + i6] = bArr[i6 + i];
            }
            return i5;
        }
    }
}
